package com.android.vmg;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class AppPreferences {
	private final int LIST_LENGTH = 5;
     private static final String APP_SHARED_PREFS = "com.android.vmg.preferences";
     private SharedPreferences appSharedPrefs;
     private Editor prefsEditor;

     public AppPreferences(Context context)
     {
         this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
         this.prefsEditor = appSharedPrefs.edit();
     }

     public List<Integer> getRanking() {
    	 List<Integer> ranking = new ArrayList<Integer> ();
    	 for(int i = 0; i < LIST_LENGTH; i++) {
    		 ranking.add(new Integer(appSharedPrefs.getInt("ranking" + Integer.valueOf(i).toString(), -1)));
    	 }
    	 return ranking;
     }

     public void saveScore(int score) {
    	 List<Integer> ranking = getRanking();
    	 for(int i = 0; i < ranking.size(); i++) {
    		 if(ranking.get(i) < score) {
    			 ranking.add(i, score);
    			 break;
    		 }
    	 }
    	 for(int i = 0; i < LIST_LENGTH; i++) {
             prefsEditor.putInt("ranking" + Integer.valueOf(i).toString(), ranking.get(i));    		 
    	 }
         prefsEditor.commit();
     }
}